package xyz.zdsoft.course_manage_module.service.imp;
import org.springframework.stereotype.Service;
import xyz.zdsoft.common.utils.Result;
import xyz.zdsoft.course_manage_module.mapper.InsertStudentToCourseMapper;
import xyz.zdsoft.course_manage_module.service.AdminInsertCourseService;
import xyz.zdsoft.course_manage_module.service.InsertStudentToCourseService;
import javax.annotation.Resource;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
@Service
public class InsertStudentToCourseServiceImpl implements InsertStudentToCourseService {
    @Resource
    private InsertStudentToCourseMapper insertStudentToCourseMapper;
    @Resource
    private AdminInsertCourseService adminInsertCourseService;
    //查询学生是否存在
    @Override
    public Integer SelHaveStudent(Integer studentId) {
        return insertStudentToCourseMapper.SelHaveStudent(studentId);
    }
    //查询学生是否已有课程
    @Override
    public Integer SelCourseHaveStudent(Integer studentId) {
        return insertStudentToCourseMapper.SelCourseHaveStudent(studentId);
    }
    //如有课程，则获取学生该课程的上课日期、教室、签退签到时间
    @Override
    public List<HashMap<String, Object>> SelCourseOnByStudentId(Integer studentId) {
        return insertStudentToCourseMapper.SelCourseOnByStudentId(studentId);
    }
    //获取当前课程的上课日期、教室、签退签到时间
    @Override
    public HashMap<String, Object> SelCourseOnById(Integer courseOnId) {
        return insertStudentToCourseMapper.SelCourseOnById(courseOnId);
    }
    //课程新增学生
    @Override
    public void InsertStudentToCourse(Integer courseOnId, Integer studentId) {
        insertStudentToCourseMapper.InsertStudentToCourse(courseOnId,studentId);
    }

    public Result toStudent(Integer courseOnId, Integer[] studentIds) throws ParseException {
        for (Integer studentId : studentIds) {
            if (SelHaveStudent(studentId) > 0) { //判断学生是否存在
                if (SelCourseHaveStudent(studentId) > 0) { //判断学生是否已有课程
                    //获取学生已有课程的信息
                    List<HashMap<String, Object>> studentCourseOns = SelCourseOnByStudentId(studentId);
                    //获取新增学生课程的信息
                    HashMap<String, Object> newCourseOn = SelCourseOnById(courseOnId);
                    //判断时间是否冲突
                    if(adminInsertCourseService.judgeDateTime(studentCourseOns,String.valueOf(newCourseOn.get("class_rome")),String.valueOf( newCourseOn.get("project_date")), String.valueOf(newCourseOn.get("sign_in_time")),String.valueOf(newCourseOn.get("sign_out_time")))>0){
                        InsertStudentToCourse(courseOnId, studentId);
                        return Result.ok().message("新增成功");
                    }
                    else return Result.error().message(studentId+"号学生在该时间端已有课程安排");

                } else {
                    InsertStudentToCourse(courseOnId, studentId);
                    return Result.ok().message("新增成功");
                }
            }
            else return Result.error().message("学生不存在");
        }
        return Result.error();
    }
}
